package com.hq.fxh.No0_ChengQu.Fragment;

import android.support.v4.view.ViewPager;
import android.view.View;

import com.hq.fxh.No0_ChengQu.Adapter.AdTemplateContentAdapter;
import com.hq.fxh.No3_TuiGuang.MyMoBan.Activity.AdBannerInfo;
import com.hq.fxh.No3_TuiGuang.MyMoBan.Activity.item.BooleanUtils;
import com.hq.fxh.CustomerView.CirclePageIndicator;
import com.hq.fxh.No3_TuiGuang.MyMoBan.Config;
import com.hq.fxh.R;

import java.util.ArrayList;
import java.util.List;


/**
 * @author 大圣
 * @ClassName AdTemplateV2Fragment
 * @Description :
 * @Created 16/6/15
 * @Email qwqe601660957@163.com
 */
public class AdTemplateV2Fragment extends BaseFragment implements ViewPager.OnPageChangeListener {
    private ViewPager viewPager;
    private CirclePageIndicator pageIndicator;
    private AdTemplateContentAdapter adTemplateContentAdapter;
    private List<AdBannerInfo> adInfos;
    private int currentPosition;
    List<AdBannerInfo> showList = new ArrayList<>();
    private boolean isSplish = false;
    private boolean isHaveBigBanner;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_ad_template_v2;
    }

    @Override
    public void findView(View view) {
//        llContent = (LinearLayout) view.findViewById(R.id.llContent);
        viewPager = (ViewPager) view.findViewById(R.id.viewPager);
        pageIndicator = (CirclePageIndicator) view.findViewById(R.id.pageIndicator);

    }

    @Override
    public void initData() {
        adInfos = new ArrayList<>();
        adTemplateContentAdapter = new AdTemplateContentAdapter(myActivity, adInfos);
        viewPager.setAdapter(adTemplateContentAdapter);
        pageIndicator.setViewPager(viewPager);
        pageIndicator.setFillColor(getResources().getColor(R.color.new_red));

        registerListener();
    }

    public void registerListener() {
        viewPager.addOnPageChangeListener(this);
    }

    //    public void showAdInfo(List<AdBannerInfo> adInfos ){
//        currentPosition = 0;
//        showAdInfo(adInfos, currentPosition);
//    }
    public void showAdInfo(List<AdBannerInfo> adInfos, int position) {
        this.adInfos = adInfos;
        currentPosition = position;
        viewPager.setAdapter(null);
        showList.clear();

        if (adInfos != null && adInfos.size() > 0 && adInfos.get(0).getAdvert_type_id() == Config.AdTypeCode.AD_BIG_BANNER && adInfos.get(0).getIs_carousel() == 1) {//大图通栏广告只在第一位（有大图通栏广告切打开）
            showList.add(adInfos.get(0));
        } else {
            for (int i = 0; i < adInfos.size(); i++) {
                adInfos.get(i).setAdTemplatePosition(i);
                if (BooleanUtils.intToBool(adInfos.get(i).getIs_carousel())) {
                    showList.add(adInfos.get(i));
                }
            }
        }

        adTemplateContentAdapter.setList(showList);
        viewPager.setAdapter(adTemplateContentAdapter);
        viewPager.setCurrentItem(position);
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        isSplish = true;
        currentPosition = showList.get(position).getAdTemplatePosition();
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    public int getCurrentPosition() {
        int position;
        if (isSplish == false) {
            if (showList != null & showList.size() > 0) {
                position = showList.get(0).getAdTemplatePosition();
            } else {
                position = 0;
            }
        } else {
            position = currentPosition;
        }
        return position;
    }

    public long getCurMerId() {
        int count = adInfos.size();
        if (count != 0) {
            if (currentPosition < 0) {
                currentPosition = 0;
            }
            return adInfos.get(getCurrentPosition()).getAdvert_type_id();
        } else {
            return 0;
        }
    }

    public List<AdBannerInfo> getList() {
        return adTemplateContentAdapter.getList();
    }

    /**
     * @param listener
     */
    public void setOnClickListener(View.OnClickListener listener) {
        adTemplateContentAdapter.setOnClickListener(listener);
    }
}
